/**
 * Project: ld.biz.service
 * 
 * File Created at 2012-9-23
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.liangdev.biz.core.autocode;

import java.util.List;
import java.util.Map;

import com.liangdev.biz.dataobject.autocode.DbInfoDO;
import com.liangdev.biz.entity.autocode.TableMetaData;
import com.liangdev.biz.service.autocode.DbInfoService;

/**
 * TODO Comment of DBMetaDataManager
 * 
 * @author chongan.wangca
 */
public class DBMetaDataManager {

    /**
     * TODO-spring inject
     */
    private Map<String, DBMetaDataCollector> dbMetaDataCollectorMap;

    private DbInfoService                    dbInfoService;

    public List<TableMetaData> getAllTables(Long dbInfoId) throws Exception {
        DbInfoDO dbInfoDO = dbInfoService.findById(dbInfoId);
        DBMetaDataCollector dbMetaDataCollector = dbMetaDataCollectorMap.get(dbInfoDO.getDbType());
        if (dbMetaDataCollector == null) {
            return null;
        }
        return dbMetaDataCollector.getAllTables(dbInfoDO.getIp(), dbInfoDO.getPort(),
                dbInfoDO.getDbName(), dbInfoDO.getUser(), dbInfoDO.getPassword());
    }

    public TableMetaData getTable(Long dbInfoId, String tableName) throws Exception {
        DbInfoDO dbInfoDO = dbInfoService.findById(dbInfoId);
        DBMetaDataCollector dbMetaDataCollector = dbMetaDataCollectorMap.get(dbInfoDO.getDbType());
        if (dbMetaDataCollector == null) {
            return null;
        }
        return dbMetaDataCollector.getTable(tableName, dbInfoDO.getIp(), dbInfoDO.getPort(),
                dbInfoDO.getDbName(), dbInfoDO.getUser(), dbInfoDO.getPassword());
    }

    public void setDbMetaDataCollectorMap(Map<String, DBMetaDataCollector> dbMetaDataCollectorMap) {
        this.dbMetaDataCollectorMap = dbMetaDataCollectorMap;
    }

    public void setDbInfoService(DbInfoService dbInfoService) {
        this.dbInfoService = dbInfoService;
    }

}
